<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-7-13
 * Time: 16:17
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;

class AdvanceController extends AdminBaseController
{

    public function index()
    {
        $param    = $this->request->param();
        $s_name = isset($param['s_name']) ? $param['s_name'] : '';
        $where = null;
        if($s_name){ $where['sl.s_name'] = ['like', "%$s_name%"];}else{ unset($param['s_name']);}
        $star = Db::name('live_list')->field('ll.id,sl.s_name,ll.l_date,ll.l_startime,ll.l_booked,ll.l_package,ll.l_gift,ll.l_status,ll.l_top')
            ->alias('ll')
            ->join('star_list sl','ll.l_udid = sl.s_udid')
            ->where($where)
            ->order(['ll.l_top'=>'ASC','ll.l_date' => 'DESC','ll.id' => 'DESC'])
            ->paginate(20);
        $star->appends($param);
        $page = $star->render();
        $this->assign("s_name", $s_name);
        $this->assign("star", $star);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function edit()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $live = DB::name('live_list')->where(["id" => $id])->find();
        $star = DB::name('star_list')->where(["s_udid" => $live['l_udid']])->find();
        $this->assign("video", $live);
        $this->assign("star", $star);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'l_package' => 'require|number',
                'l_gift' => 'require|number',
                'l_date' => 'require|dateFormat:Y-m-d',
                'l_startime' => 'require|dateFormat:H:i',
                'l_status' => 'require|number',
                'l_top' => 'require|number',
            ]);
            $validate->message([
                'l_package.require' => '红包类型不可为空',
                'l_package.number' => '红包类型不正确',
                'l_gift.require' => '礼品类型不可为空',
                'l_gift.number' => '礼品类型不正确',
                'l_date.require' => '开播日期不可为空',
                'l_date.dateFormat' => '开播日期格式不正确',
                'l_startime.require' => '开播时间不可为空',
                'l_startime.dateFormat' => '开播时间格式不正确',
                'l_status.require' => '预播状态不可为空',
                'l_status.number' => '预播状态不正确',
                'l_top.require' => '置顶状态不可为空',
                'l_top.number' => '置顶状态不正确',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                Db::name('live_list')->update($data);
                $this->success('修改成功',url('advance/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }

    }

    public function enabled()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $shout = DB::name('live_list')->where(["id" => $id])->find();
        switch ($shout['l_status']){
            case 10:
                DB::name('live_list')->where(["id" => $id])->setField('l_status', 20);
                $this->success('您已关闭此预播');
                break;
            case 20:
                DB::name('live_list')->where(["id" => $id])->setField('l_status', 10);
                $this->success('您已开启此预播');
                break;
            default:
                DB::name('live_list')->where(["id" => $id])->setField('l_status', 20);
                $this->error('未知的预播状态,已置为停用');
        }
    }

    public function top()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $shout = DB::name('live_list')->where(["id" => $id])->find();
        switch ($shout['l_top']){
            case 10:
                DB::name('live_list')->where(["id" => $id])->setField('l_top', 20);
                $this->success('您已取消置顶此预播');
                break;
            case 20:
                DB::name('live_list')->where(["id" => $id])->setField('l_top', 10);
                $this->success('您已置顶此预播');
                break;
            default:
                DB::name('live_list')->where(["id" => $id])->setField('l_top', 20);
                $this->error('未知的预播置顶状态,已置为取消置顶');
        }
    }
}